package patrol.mobile.com.mobilepatrol.activity.office;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.http.okhttp.callback.StringCallback;

import java.io.Serializable;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.bean.OtherOfficeDetailBean;
import patrol.mobile.com.mobilepatrol.bean.TaskProgressBean;
import patrol.mobile.com.mobilepatrol.utils.CommonParamsHelper;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.DatePickerUtil;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.StringUtils;
import patrol.mobile.com.mobilepatrol.utils.TimeUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.view.CallBack;

public class TaskProgressActivity extends BaseActivity {

    public static final String TAG = "TaskProgressActivity";

    @BindView(R.id.et_task_name)
    TextView etTaskName;
    @BindView(R.id.tv_jianyao_shuoming)
    EditText tvJianyaoShuoming;
    @BindView(R.id.tv_time2)
    TextView tvTime2;
    @BindView(R.id.tv_jindu)
    EditText tvJindu;
    @BindView(R.id.tv_shuoming)
    EditText tvShuoming;
    @BindView(R.id.btn_sure)
    Button btnSure;
    @BindView(R.id.btn_back)
    Button btnBack;
    private static final String IS_EDITABLE = "IS_EDITABLE";
    private static final String ID = "ID";
    private static final String MAIN_ID = "MAIN_ID";
    private String id;
    private String mainId;
    private boolean isEditable;
    private OtherOfficeDetailBean data;

    public static void show(Context context, boolean isEditable, String id, String mainId, OtherOfficeDetailBean data) {
        Intent intent = new Intent(context, TaskProgressActivity.class);
        intent.putExtra(IS_EDITABLE, isEditable);
        intent.putExtra(ID, id);
        intent.putExtra(MAIN_ID, mainId);
        intent.putExtra("DATA", data);
        context.startActivity(intent);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_task_progress;
    }

    @Override
    public void initTitle() {
        super.initTitle();
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(TaskProgressActivity.this, true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("办理进度");
    }

    @Override
    public void initView() {
        super.initView();
        isEditable = getIntent().getBooleanExtra(IS_EDITABLE, false);
        id = getIntent().getStringExtra(TaskProgressActivity.ID);
        mainId = getIntent().getStringExtra(TaskProgressActivity.MAIN_ID);
        data = (OtherOfficeDetailBean) getIntent().getSerializableExtra("DATA");
        Log.i(TAG, "initView: " + "---" + mainId);
        setEditable(isEditable);
    }

    @Override
    public void initData() {
        super.initData();
        if (!StringUtils.isEmpty(id)) {
            requestData();
        } else {
            etTaskName.setText(data.getTitle());
            tvTime2.setText(TimeUtils.dateString(System.currentTimeMillis()));
            tvTime2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DatePickerUtil.getInstance(TaskProgressActivity.this).setCallBack(new CallBack<DateBean>() {
                        @Override
                        public void onBack(DateBean dateBean) {
                            tvTime2.setText(dateBean.getmDate());
                        }
                    });
                }
            });
        }
    }

    private void requestData() {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.TASK_SCHEDULE_ENTITY);
        sender.requestPost("id", id, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                TaskProgressBean taskProgressBean = GsonUtil.GsonToBean(response, TaskProgressBean.class);
                etTaskName.setText(taskProgressBean.getTaskName());
                tvJianyaoShuoming.setText(taskProgressBean.getTitle());
                tvTime2.setText(taskProgressBean.getUpDate());
                tvJindu.setText(taskProgressBean.getCompletion() + "%");
                tvShuoming.setText(taskProgressBean.getContext());
            }
        });
    }

    @Override
    public void setListener() {
    }

    @OnClick({R.id.btn_sure, R.id.btn_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_sure:
                submit();
                break;
            case R.id.btn_back:
                finish();
                break;
        }
    }

    private void submit() {
        String name = CommonUtil.getText(etTaskName);
        String jianyaoshuoming = CommonUtil.getText(tvJianyaoShuoming);
        String time = CommonUtil.getText(tvTime2);
        String jindu = CommonUtil.getText(tvJindu);
        String shuoming = CommonUtil.getText(tvShuoming);
        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(jianyaoshuoming) || StringUtils.isEmpty(time) || StringUtils.isEmpty(jindu) || StringUtils.isEmpty(shuoming)) {
            Toast.makeText(this, "请填写相关内容", Toast.LENGTH_SHORT).show();
            return;
        }
        HttpSender sender = new HttpSender(this, baseUrl, Urls.TASK_SCHEDULE_UP_LOAD);
        HashMap<String, String> idDateBmnameMap = CommonParamsHelper.getIdDateBmnameMap();
        HashMap<String, String> mainIdMap = new HashMap<>();
        mainIdMap.put("id", mainId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("taskName", name);
        map.put("title", jianyaoshuoming);
        map.put("upDate", time);
        map.put("completion", jindu);
        map.put("context", shuoming);
        map.put("mainId", mainIdMap);
        map.putAll(idDateBmnameMap);
        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("userSchedule", GsonUtil.map2Json(map));
        sender.postJson(GsonUtil.map2Json(map1), new StringCallback() {

            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                Log.i(TAG, "onResponse: " + response);
                finish();
            }
        });
    }

    private void setEditable(boolean isEditable) {
        etTaskName.setEnabled(isEditable);
        tvJianyaoShuoming.setEnabled(isEditable);
        tvTime2.setEnabled(isEditable);
        tvJindu.setEnabled(isEditable);
        tvShuoming.setEnabled(isEditable);
        if (isEditable) {
            btnSure.setVisibility(View.VISIBLE);
        } else {
            btnSure.setVisibility(View.GONE);
        }
    }
}
